package GUI.calendar;
import java.awt.Color;
import java.awt.Font;
import java.awt.FontMetrics;
import java.awt.Graphics;

import javax.swing.BorderFactory;
import javax.swing.JPanel;


public class CalendarGridCellPanel extends JPanel {

	private static final long serialVersionUID = 1L;
	
	private static final Color NONEXISTENT_COLOR = new Color(50, 180, 230);
	private static final Font DATE_FONT = new Font(Font.SANS_SERIF, Font.PLAIN, 10);
	
	private int dayOfMonth;
	private boolean exists;
	private boolean booked;
	
	public CalendarGridCellPanel(int dayOfMonth, boolean exists, boolean booked) {		
		this.dayOfMonth = dayOfMonth;
		this.exists = exists;
		this.booked = booked;
		
		setBackground(Color.WHITE);
		setBorder(BorderFactory.createLineBorder(Color.BLACK, 1));
	}
	
	public void paintComponent(Graphics g) {
		super.paintComponent(g);
		
		if (!exists) {
			g.setColor(NONEXISTENT_COLOR);
			g.fillRect(0, 0, getWidth(), getHeight());
			return;
		}

		g.setColor(Color.BLACK);
		
		if (booked) {
			int h = getHeight();
			for (int x = 0; x < getWidth() + h; x += 10) {
				g.drawLine(x, 0, x - h, h);
			}
		}
		
		int w = getWidth();
		g.setColor(Color.WHITE);
		g.fillRect(w - 20, 0, 20, 16);
		g.setColor(Color.BLACK);
		g.drawLine(w - 20, 0, w - 20, 16);
		g.drawLine(w - 20, 16, w, 16);
		String dateString = String.valueOf(dayOfMonth + 1);
		g.setFont(DATE_FONT);
		FontMetrics fm = g.getFontMetrics();
		int width = fm.stringWidth(dateString);
		int height = DATE_FONT.getSize();
		int x = (20 - width) / 2;
		int y = (16 - height) / 2;
		g.drawString(dateString, w - 20 + x, 16 - y);
	}
	
	
	public int getDayOfMonth() {
		return dayOfMonth;
	}

}
